package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.latticy.common.enumeration.CategoryRootOrNotEnum;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.CategoryDTO;
import io.github.talelin.latticy.service.CategoryService;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.model.CategoryDO;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;
import java.util.List;

/**
 * @author generator@TaleLin
 * @since 2021-06-19
 */
@RestController
@RequestMapping("/v1/category")
@Validated
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @PostMapping("")
    public CreatedVO create(@RequestBody @Validated CategoryDTO categoryDTO) {
        CategoryDO categoryDO = new CategoryDO();
        BeanUtils.copyProperties(categoryDTO, categoryDO);
        this.categoryService.save(categoryDO);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Long id,
                            @RequestBody @Validated CategoryDTO categoryDTO) {
        CategoryDO categoryDO = this.categoryService.getCategoryById(id.intValue());
        if (categoryDO == null) {
            throw new NotFoundException(40000);
        }
        BeanUtils.copyProperties(categoryDTO, categoryDO);
        this.categoryService.getBaseMapper().updateById(categoryDO);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Long id) {
        this.categoryService.deleteCategory(id.intValue());
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    public CategoryDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Long id) {
        CategoryDO categoryDO = this.categoryService.getCategoryById(id.intValue());
        if (categoryDO == null) {
            throw new NotFoundException(40000);
        }
        return categoryDO;
    }

    @GetMapping("/page")
    public PageResponseVO<CategoryDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Long count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Long page,
            @RequestParam(name = "root") @Min(value = 0) @Max(value = 1) Integer root
    ) {
        IPage<CategoryDO> paging = this.categoryService.getCategoriesByPage(count, page, root);


        return PageUtil.build(paging);
    }

    @GetMapping("/sub-page")
    public PageResponseVO<CategoryDO> subPage(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Long count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Long page,
            @RequestParam(name = "id") @Positive(message = "{id}") Integer id
    ) {

        IPage<CategoryDO> paging = this.categoryService.getSubCategoriesByPage(count, page, id);

        return PageUtil.build(paging);
    }

    @GetMapping("/list")
    public List<CategoryDO> getList() {
        val notRoot = CategoryRootOrNotEnum.NOT_ROOT;
        return this.categoryService.lambdaQuery().eq(CategoryDO::getIsRoot, notRoot).list();
    }

}
